﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.ships.cruise {
    import flash.events.*;
    import Engine.Managers.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.effects.*;
    import Classes.LogicComponents.*;
    import Modules.quest.Managers.*;
    import Classes.doobers.*;
    import Classes.inventory.*;
    import Modules.stats.experiments.*;
    import Display.PopulationUI.*;
    import Modules.stats.types.*;

    public class CruiseShipLogicComponent extends HarvestableShipComponentBase {

        private const DESIRED_COMMODITY:String = "premium_goods";

        protected var m_doobersToGiveAway:Array = null;
        protected var m_isDelivering:Boolean = false;
        protected var m_isHarvesting:Boolean = false;

        public function CruiseShipLogicComponent(_arg1:HarvestableShip){
            super(_arg1);
            var _local2:XML = Global.gameSettings.getEffectByName("fireworks");
            var _local3:XML = _local2.image[0];
            var _local4:ItemImage = new ItemImage(_local3);
            _local4.load();
            Sounds.loadSoundByName("cruise_arrive_nocamera");
            Sounds.loadSoundByName("ship_approaches");
            Sounds.loadSoundByName("cruise_fireworks");
        }
        override public function getProgressBarStartFunction():Function{
            return (function ():Boolean{
                if (Global.isVisiting()){
                    return (true);
                };
                switch (m_ship.getState()){
                    case HarvestableResource.STATE_GROWN:
                        if (m_ship.isHarvestable()){
                            Sounds.playFromSet(Sounds.SET_HARVEST, m_ship);
                            spawnTourists();
                            return (true);
                        };
                        return (false);
                    case HarvestableResource.STATE_PLOWED:
                        if (Global.player.commodities.getAddedCount(m_ship.commodities) >= m_ship.harvestingDefinition.commodityReq){
                            return (true);
                        };
                        return (false);
                    default:
                        return (true);
                };
            });
        }
        override public function getProgressBarEndFunction():Function{
            return (function ():void{
                var _local1:*;
                if (Global.isVisiting()){
                    return;
                };
                switch (m_ship.getState()){
                    case HarvestableResource.STATE_GROWN:
                        if (m_ship.harvest()){
                            m_ship.doHarvestDropOff();
                        };
                        m_ship.setState(HarvestableResource.STATE_PLOWED);
                        m_ship.clearSupply();
                        m_isHarvesting = false;
                        break;
                    case HarvestableResource.STATE_PLOWED:
                        Sounds.play("ship_approaches");
                        _local1 = DESIRED_COMMODITY;
                        m_ship.addSupply(_local1, m_ship.harvestingDefinition.commodityReq);
                        GameTransactionManager.addTransaction(new TSupplyShip(m_ship, _local1));
                        m_ship.setState(HarvestableResource.STATE_PLANTED);
                        m_ship.plantTime = GlobalEngine.getTimer();
                        startShipLeave();
                        m_isDelivering = false;
                        break;
                };
            });
        }
        override public function getProgressBarCancelFunction():Function{
            return (function ():void{
                m_isHarvesting = false;
                m_isDelivering = false;
            });
        }
        override public function harvest():Boolean{
            var _local1:Boolean;
            if (m_ship.isHarvestable()){
                if (!Global.isVisiting()){
                    m_ship.doDoobers();
                    if (m_ship.actionMode != m_ship.VISIT_REPLAY_ACTION){
                        GameTransactionManager.addTransaction(new THarvest(m_ship));
                        m_ship.trackAction(TrackedActionType.HARVEST);
                    };
                };
                _local1 = true;
            };
            m_ship.harvestCounter++;
            if (Global.experimentManager.getVariant(ExperimentDefinitions.SWEEPSTAKES_CRUISE) == 1){
                StatsManager.count(StatsCounterType.SWEEPSTAKES, StatsKingdomType.CRUISESHIPS, "", "", "", "", m_ship.harvestCounter);
            };
            m_ship.updateObjectIndicator();
            return (_local1);
        }
        override public function makeDoobers(_arg1:Item, _arg2:Array, _arg3:Number=1):Array{
            var _local5:Array;
            var _local6:String;
            var _local7:int;
            this.m_doobersToGiveAway = super.makeDoobers(_arg1, _arg2, _arg3);
            var _local4:Number = m_ship.getSupplyBonus();
            for each (_local5 in this.m_doobersToGiveAway) {
                _local6 = _local5[0];
                _local7 = _local5[1];
                if (_local6 == Global.gameSettings.getDooberFromType(Doober.DOOBER_COIN, _local7)){
                    _local5[1] = (_local7 * _local4);
                };
            };
            return (this.m_doobersToGiveAway);
        }
        override public function handlePlayAction():void{
            if (Global.isVisiting()){
                return;
            };
            switch (m_ship.getState()){
                case HarvestableResource.STATE_GROWN:
                    if (((((m_ship.isHarvestable()) && (!(Global.guide.isActive())))) && (!(this.m_isHarvesting)))){
                        this.m_isHarvesting = true;
                        if (this.isUxLockedByQuest()){
                            Global.ui.showTickerMessage(ZLoc.t("Main", "TickerCruiseShips"));
                        };
                        Sounds.play("cruise_arrive_nocamera");
                        if (m_ship.harvestCounter == 0){
                            TimerUtil.callLater(function ():void{
                                fireZeMissiles(null);
                            }, 1000);
                        };
                        super.handlePlayAction();
                    };
                    break;
                case HarvestableResource.STATE_PLOWED:
                    if (((!(this.m_isHarvesting)) && ((Global.player.commodities.getAddedCount(m_ship.commodities) >= m_ship.harvestingDefinition.commodityReq)))){
                        this.m_isDelivering = true;
                        m_ship.removeStagePickEffect();
                        Global.world.citySim.pickupManager.enqueue("NPC_shipPickup", m_ship);
                    } else {
                        ResourceExplanationDialog.show(ResourceExplanationDialog.TYPE_NOT_ENOUGH_GOODS, true);
                    };
                    break;
            };
        }
        private function fireZeMissiles(_arg1:Event=null):void{
            var e = _arg1;
            Sounds.play("cruise_fireworks");
            MapResourceEffectFactory.createEffect(m_ship, EffectType.FIREWORK_BALLOONS);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(m_ship, EffectType.FIREWORK_BALLOONS);
            }, 300);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(m_ship, EffectType.FIREWORK_BALLOONS);
            }, 500);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(m_ship, EffectType.FIREWORK_BALLOONS);
            }, 700);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(m_ship, EffectType.FIREWORK_BALLOONS);
            }, 900);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(m_ship, EffectType.FIREWORK_BALLOONS);
            }, 1000);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(m_ship, EffectType.FIREWORK_BALLOONS);
            }, 1200);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(m_ship, EffectType.FIREWORK_BALLOONS);
            }, 1600);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(m_ship, EffectType.FIREWORK_BALLOONS);
            }, 1300);
        }
        protected function spawnTourists():void{
            var _local1:int = int(m_ship.harvestingDefinition.xml.cruiseTourists);
            Global.world.citySim.cruiseShipManager.releasePeeps(_local1, m_ship);
            Global.world.citySim.cruiseShipManager.removeCheeringScene();
        }
        override public function getToolTipAction():String{
            var _local1:String = super.getToolTipAction();
            if (Global.isVisiting()){
                return (_local1);
            };
            switch (m_ship.getState()){
                case HarvestableResource.STATE_GROWN:
                    if (this.m_isHarvesting){
                        break;
                    };
                    if (!Global.player.checkEnergy(-(m_ship.harvestingDefinition.harvestEnergyCost), false)){
                        _local1 = ZLoc.t("Main", "NeedEnergy", {amount:m_ship.harvestingDefinition.harvestEnergyCost});
                    } else {
                        _local1 = ZLoc.t("Main", "LoadedAction");
                    };
                    break;
                case HarvestableResource.STATE_PLOWED:
                    if (this.m_isDelivering){
                        break;
                    };
                    if (Global.player.commodities.getAddedCount(m_ship.commodities) >= m_ship.harvestingDefinition.commodityReq){
                        _local1 = ZLoc.t("Main", "ClickToSupply");
                    } else {
                        _local1 = Commodities.getNoCommoditiesTooltip();
                    };
                    break;
            };
            return (_local1);
        }
        override public function getToolTipStatus():String{
            var _local2:String;
            var _local1 = "";
            if (Global.isVisiting()){
                return (_local1);
            };
            switch (m_ship.getState()){
                case HarvestableResource.STATE_PLANTED:
                    _local2 = GameUtil.formatMinutesSeconds(m_ship.getGrowTimeLeft());
                    _local1 = ZLoc.t("Main", "AwayCruise", {time:_local2});
                    break;
                case HarvestableResource.STATE_PLOWED:
                    if (this.m_isDelivering){
                        break;
                    };
                    _local1 = ZLoc.t("Main", "NeedCommodityToOpenAmount", {amount:m_ship.harvestingDefinition.commodityReq});
                    break;
            };
            return (_local1);
        }
        override public function onStateChanged(_arg1:String, _arg2:String):void{
            super.onStateChanged(_arg1, _arg2);
            this.checkCheeringScene();
            if (((!((_arg1 == _arg2))) && ((_arg2 == HarvestableResource.STATE_GROWN)))){
                Sounds.play("ship_approaches");
            };
        }
        public function checkCheeringScene():void{
            if ((((((m_ship.harvestCounter == 0)) && ((m_ship.getState() == HarvestableResource.STATE_GROWN)))) && (!(Global.world.citySim.cruiseShipManager.isCheeringSceneActive())))){
                Global.world.citySim.cruiseShipManager.createCheeringScene();
            };
        }
        protected function isUxLockedByQuest():Boolean{
            return (!(Global.questManager.isFlagReached(GameQuestManager.QUEST_UX_CRUISESHIP)));
        }
        override public function getHarvestTime():Number{
            switch (m_ship.getState()){
                case HarvestableResource.STATE_GROWN:
                    return (Global.gameSettings.getNumber("actionBarCruiseUnload"));
                case HarvestableResource.STATE_PLOWED:
                default:
                    return (Global.gameSettings.getNumber("actionBarBizOpen"));
            };
        }
        override public function getActionText():String{
            switch (m_ship.getState()){
                case HarvestableResource.STATE_GROWN:
                    return (ZLoc.t("Main", "Unloading"));
                case HarvestableResource.STATE_PLOWED:
                default:
                    return (ZLoc.t("Main", "BusinessOpening"));
            };
        }
        override public function enableUpdateArrow():Boolean{
            return (((!(Global.isVisiting())) && ((((m_ship.getState() == HarvestableResource.STATE_GROWN)) || ((m_ship.getState() == HarvestableResource.STATE_PLOWED))))));
        }
        override public function createStagePickEffect():void{
            if (m_ship.getState() == HarvestableResource.STATE_GROWN){
                createStagePickEffectHelper(StagePickEffect.PICK_1);
            } else {
                if (m_ship.getState() == HarvestableResource.STATE_PLOWED){
                    createStagePickEffectHelper(StagePickEffect.PICK_GOODS);
                };
            };
        }
        override public function isSellable():Boolean{
            return (false);
        }

    }
}//package Modules.ships.cruise 
